package cn.nuc.edu.onlinestore.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.nuc.edu.onlinestore.dao.GoodsMapper;
import cn.nuc.edu.onlinestore.dao.UserMapper;
import cn.nuc.edu.onlinestore.entity.Goods;
import cn.nuc.edu.onlinestore.entity.Sysuser;
import cn.nuc.edu.onlinestore.service.interfaces.IGoodsService;
import cn.nuc.edu.onlinestore.util.PageControl;
import cn.nuc.edu.onlinestore.util.Pager;

/**
 * 商品相关业务逻辑
 * @author 王凯
 *
 */
@Service
public class GoodsService implements IGoodsService {
	
	/**
	 * 商品映射器
	 */
	@Autowired
	private GoodsMapper goodsMapper;
	
	/**
	 * 用户映射器
	 */
	@Autowired
	private UserMapper usermapper;

	/**
	 * 获得商品信息
	 */
	@Override
	public Goods detail(Goods goods) {
		return goodsMapper.detail(goods);
	}

	/**
	 * 分页查询商品信息
	 */
	@Override
	public Pager<Goods> list(String keyword, PageControl pc, boolean condition) {
		
		//获取封装的分页参数
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("currentindex", pc.getCurrentindex());
		paramMap.put("pagesize", pc.getPagesize());
		paramMap.put("keyword", keyword);
		
		//如果要过滤掉已经下架的商品，condition会被置为true，表示开启过滤
		if (condition == true) {
			paramMap.put("condition", "true");
		}
		
		//执行分页查询操作
		List<Goods> funlist = goodsMapper.list(paramMap);
		
		//获得总记录数
		Integer count = goodsMapper.getAllCount();
		
		//封装分页信息
		Pager<Goods> pager = new Pager<>();
		pager.setList(funlist);
		pc.setRscount(count);
		pager.setPagectrl(pc);
		
		return pager;
	}

	/**
	 * 添加商品信息
	 */
	@Override
	@Transactional
	public Integer addGoods(Goods goods) {
		return goodsMapper.addGoods(goods);
	}

	/**
	 * 修改商品信息
	 */
	@Override
	@Transactional
	public Integer edit(Goods goods) {
		return goodsMapper.edit(goods);
	}

	/**
	 * 商品上架
	 */
	@Override
	@Transactional
	public Integer hitShelves(Goods goods){
		return goodsMapper.hitShelves(goods);
	}
	
	/**
	 * 添加购物记录
	 */
	@Override
	@Transactional
	public Integer addConsumerRecord(Sysuser user, Goods goods, Integer count) {
		
		//先查一下货物的库存，看够不够
		Goods detail = goodsMapper.detail(goods);
		if (goods.getGcount() >= count) { //库存够
			//再检查客户的账户余额够不够
			Sysuser searchuser = usermapper.detail(user);
			double account = searchuser.getUseraccount();
			double cost = goods.getGprice() * count;
			if (account >= cost) {
				//扣减库存
				detail.setGcount(detail.getGcount() - count);
				goodsMapper.buy(detail);
				
				//扣减账户余额
				searchuser.setUseraccount((float) (searchuser.getUseraccount() - cost));
				usermapper.alterAccount(searchuser);
				
				//组装参数
				Map<String, Object> paramMap = new HashMap<>();
				paramMap.put("userid", user.getUserid());
				paramMap.put("goodsid", goods.getGid());
				paramMap.put("buycost", cost);
				paramMap.put("buytime", new Date());
				
				//增加购物记录
				return goodsMapper.addConsumerRecord(paramMap);
			}
		}
		return 0;
	}

	/**
	 * 得到用户的消费列表
	 */
	@Override
	public List<Goods> getConsumerList(Sysuser user) {
		return goodsMapper.getConsumerList(user);
	}

	/**
	 * 得到上架清单
	 * @return
	 */
	public List<Goods> gethitlist() {
		return goodsMapper.gethitlist();
	}
}
